<?php

function add_wall($dbc, $uid, $name){
	$wid = rand_str();
	$query = "insert into user_walls (user_id, wall_id , name, created_on) values ('$uid','$wid','$name',now())";
	$dbc->execute_query($query);
	return $wid;
}

function get_wall_list($dbc, $uid){
	$query = "select wall_id, name from user_walls where user_id = '$uid' order by created_on";
	$result = $dbc->execute_query($query);
	if($result){
		$res = array();
		while($row = mysql_fetch_array($result)){
			$res[] = array($row['wall_id'],$row['name'] );
		}
		return $res;
	}else {
		return array();
	}
}
function add_wall_notes($dbc, $uid, $wid, $tag_list){
	$npid = rand_str();
	$query = "insert into wall_notes (user_id, wall_id, notepad_id, tag_list, position, created_on) values('$uid','$wid','$npid','$tag_list','0px,0px',now())";
	$dbc->execute_query($query);
	$res = array();
	$urow = array();
	$urow['notepad_id'] = $npid;
	$urow['tag_list'] = $tag_list;
	$urow['position'] = '0px,0px';
	$tags = str_to_array($tag_list);
	$urow['notepad'] = get_tag_notes($dbc, $uid, $tags);
	$res[] = $urow;
	return $res;
}

function get_wall_notes($dbc, $uid, $wid){
	$query = "select notepad_id, tag_list, position from wall_notes where user_id = '$uid' and wall_id = '$wid' order by created_on ";
	$result = $dbc->execute_query($query);
	if($result){
		$res = array();
		while($row = mysql_fetch_array($result)){
			$urow = array();
			$urow['notepad_id'] = $row['notepad_id'];
			$urow['tag_list'] = $row['tag_list'];
			$urow['position'] = $row['position'];
			
			$tags = str_to_array($urow['tag_list']);
			$urow['notepad'] = get_tag_notes($dbc, $uid, $tags);
			$res[] = $urow;
		}
		return $res;
	}else {
		return null;
	}
}


function update_note_position($dbc, $uid, $wid, $npid, $pos){
	$query = "update wall_notes set position = '$pos' where user_id = '$uid' and wall_id = '$wid' and notepad_id = '$npid' ";
	$dbc->execute_query($query);
}
?>
